package com.ocom.payconfig.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfPointInfo;
import com.ocom.common.entity.payconfig.EntityDishesClass;
import com.ocom.common.entity.payconfig.EntityMqttDevice;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.emqx.YhDeviceWorkStatusRequest;
import com.ocom.common.request.payconfig.IotDeviceListRequest;
import com.ocom.common.request.payconfig.NewIotDeviceRequest;
import com.ocom.common.request.payconfig.RemoveIotDeviceRequest;
import com.ocom.common.request.payconfig.UpdateIotDeviceRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.response.payconfig.IotDeviceListVo;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.emqx.YhDeviceWorkStatusVo;
import com.ocom.payconfig.client.EmqxClientService;
import com.ocom.payconfig.service.DishesClassService;
import com.ocom.payconfig.service.MqttDeviceService;
import com.ocom.payconfig.service.XfPointInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class IotDeviceController {
    @Autowired
    private MqttDeviceService mqttDeviceService;

    @Autowired
    private XfPointInfoService xfPointInfoService;

    @Autowired
    private DishesClassService dishesClassService;

    @Autowired
    private EmqxClientService emqxClientService;

    @PostMapping(PayConfigUrl.URL_newIotDevice)
    public Result newIotDevice(@RequestBody NewIotDeviceRequest newIotDeviceRequest){
        EntityMqttDevice entityMqttDevice = Convert.convert(EntityMqttDevice.class,newIotDeviceRequest);
        EntityMqttDevice entityMqttDeviceA = this.mqttDeviceService.selectDevice(entityMqttDevice.getComId(),entityMqttDevice);
        if(ObjectUtil.isNotEmpty(entityMqttDeviceA)){
            return ResultUtil.error(ResultEnum.Err_6001);
        }
        if(entityMqttDevice.getMqttDeviceType().equals(2)){
            entityMqttDevice.setSubNum(2);
        }
        this.mqttDeviceService.save(entityMqttDevice.getComId(),entityMqttDevice);
        return ResultUtil.success();
    }

    @PostMapping(PayConfigUrl.URL_updateIotDevice)
    public Result updateIotDevice(@RequestBody UpdateIotDeviceRequest updateIotDeviceRequest){
        EntityMqttDevice entityMqttDevice = Convert.convert(EntityMqttDevice.class,updateIotDeviceRequest);
        this.mqttDeviceService.update(entityMqttDevice.getComId(),entityMqttDevice);
        return ResultUtil.success();
    }


    @PostMapping(PayConfigUrl.URL_iotDeviceDetail)
    public Result iotDeviceDetail(@RequestBody JSONObject jsonObject){
        EntityMqttDevice entityMqttDevice = new EntityMqttDevice();
        entityMqttDevice.setComId(jsonObject.getLong("comId"));
        entityMqttDevice.setMqttDeviceNo(jsonObject.getString("mqttDeviceNo"));
        return ResultUtil.success(this.mqttDeviceService.selectDevice(entityMqttDevice.getComId(),entityMqttDevice));
    }

    @PostMapping(PayConfigUrl.URL_removeIotDevice)
    public Result removeIotDevice(@RequestBody RemoveIotDeviceRequest request){
        EntityMqttDevice entityMqttDevice = Convert.convert(EntityMqttDevice.class,request);
        this.mqttDeviceService.remove(entityMqttDevice.getComId(),entityMqttDevice);
        return ResultUtil.success();
    }

    @PostMapping(PayConfigUrl.URL_iotDeviceList)
    public Result iotDeviceList(@RequestBody IotDeviceListRequest request){
        EntityMqttDevice entityMqttDevice = Convert.convert(EntityMqttDevice.class,request);

        PageInfo<EntityMqttDevice> page= this.mqttDeviceService.page(entityMqttDevice.getComId(),request,entityMqttDevice);
        List<IotDeviceListVo> list = new ArrayList<>();
        page.getList().forEach(device -> {
            YhDeviceWorkStatusRequest deviceWorkStatusRequest = new YhDeviceWorkStatusRequest();
            deviceWorkStatusRequest.setStatus(entityMqttDevice.getDeviceStatus());
            deviceWorkStatusRequest.setSubRun(entityMqttDevice.getSubRun());
            deviceWorkStatusRequest.setSubNum(entityMqttDevice.getSubNum());

            Result<YhDeviceWorkStatusVo> resultA= this.emqxClientService.yhDeviceWorkStatus(deviceWorkStatusRequest);
            YhDeviceWorkStatusVo yhDeviceWorkStatusVo = resultA.getData();
            device.setDeviceStatus(yhDeviceWorkStatusVo.getRunState());
            IotDeviceListVo iotDeviceListVo = Convert.convert(IotDeviceListVo.class,device);
            EntityXfPointInfo entityXfPointInfo = xfPointInfoService.getTollPointInfo(device.getComId(),device.getPointId());
            EntityDishesClass entityDishesClass = dishesClassService.selectDishesClassId(device.getComId(), device.getMerchId(),device.getDishesClassId());
            if(ObjectUtil.isNotEmpty(entityXfPointInfo)){
                iotDeviceListVo.setPointName(entityXfPointInfo.getPointName());
            }

            if(ObjectUtil.isNotEmpty(entityDishesClass)){
                iotDeviceListVo.setDishesClassName(entityDishesClass.getDishesClassName());
            }

            list.add(iotDeviceListVo);
        });

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("list",list);
        resultMap.put("total",page.getTotal());
        return ResultUtil.success(resultMap);
    }
}
